﻿namespace EyeOpen.TimeAssistant.Server.BootStrapper
{
    using EyeOpen.Data;
    using EyeOpen.Data.Nhibernate;
    using EyeOpen.Data.Nibernate.Mapping;
    using EyeOpen.Dependency;
    using EyeOpen.TimeAssistant.Data.Nhibernate.Mapping;

    internal class TimeAssistantContainerConfigurator : IContainerConfigurator
    {
        public const string TimeAssistantConfigurationName = "TimeAssistantConnection";

        public void Configure(IDependencyContainer container)
        {
            RegisterTimeAssistantDataContext(container);
        }

        private static void RegisterTimeAssistantDataContext(IDependencyContainer container)
        {
            var mappings = new[] { typeof(UniqueEntityMap<>).Assembly, typeof(CredentialMap).Assembly, typeof(UserStoryTaskMap).Assembly };

            var timeAssistantSqlFluentConfiguration = 
                new FluentConfigurationHelper()
                .CreateFluentConfiguration(TimeAssistantConfigurationName, mappings);
            var sessionFactory = timeAssistantSqlFluentConfiguration.BuildSessionFactory();
            
            timeAssistantSqlFluentConfiguration.BuildConfiguration();

            container.RegisterInstance(TimeAssistantConfigurationName, sessionFactory);

            var timeAssistantDataBase = new NhibernateDataBase(timeAssistantSqlFluentConfiguration);
            container.RegisterInstance<IDatabase>(TimeAssistantConfigurationName, timeAssistantDataBase);
        }
    }
}